
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>使用 Redux-Saga 管理 React 应用中的异步 action （上） - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,前言可以用Don’t call me, I’ll call you这句话来比喻react-saga。
Generators 先行！为了理解 Sagas，我们首先要理解什么是 Generator。下面,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">使用 Redux-Saga 管理 React 应用中的异步 action （上）</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/React-saga"><b>「
                    </b>REACT-SAGA<b> 」</b></a>
                
                August 21, 2018
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2018/08/21/React/React-saga/%E4%BD%BF%E7%94%A8-Redux-Saga-%E7%AE%A1%E7%90%86-React-%E5%BA%94%E7%94%A8%E4%B8%AD%E7%9A%84%E5%BC%82%E6%AD%A5-action-%EF%BC%88%E4%B8%8A%EF%BC%89/" title="使用 Redux-Saga 管理 React 应用中的异步 action （上）" class="">使用 Redux-Saga 管理 React 应用中的异步 action （上）</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    15k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    13 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/React-saga/" rel="tag">React-saga</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>可以用<code>Don’t call me, I’ll call you</code>这句话来比喻<code>react-saga</code>。</p>
<h2 id="Generators-先行！"><a href="#Generators-先行！" class="headerlink" title="Generators 先行！"></a>Generators 先行！</h2><p>为了理解 Sagas，我们首先要理解什么是 <code>Generator</code>。下面是 <a href="https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FStatements%2Ffunction%252A">MDN</a> 对 Generator 的描述：</p>
<blockquote>
<p>Generator 是在执行时能暂停，后面又能从暂停处继续执行的函数。它的上下文会在继续执行时保存。</p>
</blockquote>
<p>你可以把 Generator 理解成一种遍历器对象生成函数，（译注：Generator 执行后返回的遍历器对象）提供一个 <code>next</code> 方法。执行这个方法就会返回下一个状态，或者返回遍历结束的状态。这就需要 Generator 能够维护内部状态。</p>
<p>下面是一个基本的 Generator 示例，它生成的遍历器对象会返回几个字符串：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> * <span class="title">helloWorld</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> hello;</span><br><span class="line">    <span class="keyword">yield</span> world;</span><br><span class="line">    <span class="keyword">return</span> helloWorld;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> f = helloWorld();</span><br><span class="line">f.next() <span class="comment">// &#123;value:'hello',done:false&#125;</span></span><br><span class="line">f.next() <span class="comment">// &#123;value:'world',done:false&#125;</span></span><br><span class="line">f.next() <span class="comment">// &#123;value:'helloWorld',done:true&#125;</span></span><br></pre></td></tr></table></figure>
<p>next 方法的返回值结构非常简单 — 只要我们通过 yield/return 返回值，这个返回值就是 value 属性的值。如果我们没有返回值，value 属性的值就是 undefined，done 属性的值就是 true。<br>还有一点值的注意的是，执行 helloWorld 后，函数会在调用 yield 的地方停下来。当我们调用 next 方法后，函数会继续执行，直到遇到下一个 yield。如果我们调用了 return 语句或者函数执行完毕，done 属性就会为真。</p>
<ul>
<li>如果状态序列的长度不确定时，我们可以用下面的方法来写：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> results = generator.next();</span><br><span class="line"><span class="keyword">while</span>(!results.done)&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(results.value);</span><br><span class="line"> results = generator.next();</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(results.value);</span><br></pre></td></tr></table></figure>
<h2 id="什么是-Sagas？"><a href="#什么是-Sagas？" class="headerlink" title="什么是 Sagas？"></a>什么是 Sagas？</h2><p>Sagas 是通过 Generator 函数来创建的。<a href="https://link.juejin.im/?target=https%3A%2F%2Fgithub.com%2Fredux-saga%2Fredux-saga">官方文档</a> 的解释如下：</p>
<blockquote>
<p>Saga 就像应用中的一个独立线程，完全负责管理异步 action。</p>
</blockquote>
<p>你可以把 Saga 想象成一个以最快速度不断地调用 next 方法并尝试获取所有 yield 表达式值的线程。你可能会问这和 React 有什么关系，为什么要使用它，所以首先来看看如何在 React &amp; Redux 应用使用 Saga：<br>在 React &amp; Redux 应用中，一个常见的用法从调用一个 action 开始。被分配用来处理这个 action 的 reducer 会使用新的 state 更新 store，随后视图就会被更新渲染。<br><a id="more"></a></p>
<p>如果一个 Saga 被分配用来处理这个 action — 这个 action 通常就是个异步 action（比如一个对服务端的请求），一旦这个 action 完成后，Saga 会调用另一个 action 让 reducer 进行处理。</p>
<h2 id="常见用例"><a href="#常见用例" class="headerlink" title="常见用例"></a>常见用例</h2><p>我们可以通过一个常见流程来说明：<br>用户与页面进行交互，这个交互动作会触发一个从服务端请求数据的动作(此时页面显示 loading 提示)，最终我们用请求回来的数据去渲染页面的内容。<br>让我们为每步创建一个 action，然后用 Redux-Saga 实现一个简化的版本如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// saga.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; take &#125; <span class="keyword">from</span> <span class="string">'redux-saga/effects'</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">mySaga</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> take(USER_INTERACTED_WITH_UI_ACTION);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这个 Saga 的函数名叫做 <code>mySaga</code>。它调用了 Redux-Saga <a href="https://link.juejin.im/?target=https%3A%2F%2Fredux-saga.js.org%2Fdocs%2Fapi%2F%23effect-creators">effect</a> 的 <code>take</code> 方法，这个方法会阻塞 Saga 的执行，直到有人调用了作为参数的那个 action，Saga 的执行也会结束，就像我们前面看到的 Generator 一样（done 变为 true）。</p>
<p>现在我们要让页面展示 loading 提示来响应这个 action。可以通过 <code>put</code> 方法调用另一个 action，然后分配 reducer 来处理，从而完成上述功能。如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// saga.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; take, put &#125; <span class="keyword">from</span> <span class="string">'redux-saga/effects'</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">mySaga</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> take(USER_INTERACTED_WITH_UI_ACTION);</span><br><span class="line">    <span class="keyword">yield</span> put(SHOW_LOADING_ACTION, &#123;<span class="attr">isLoading</span>: <span class="literal">true</span>&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// reducer.js</span></span><br><span class="line">...</span><br><span class="line"><span class="keyword">case</span> SHOW_LOADING_ACTION: <span class="function">(<span class="params">state, isLoading</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Object</span>.assign(&#123;&#125;, state, &#123;<span class="attr">showLoading</span>: isLoading&#125;);</span><br><span class="line">&#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>下一步是调用 <code>call</code> 方法，它接收一个函数和一组参数，使用这些参数来执行这个函数。我们给 <code>call</code> 方法传递一个请求服务端并返回一个 Promise 的 <code>GET</code> 函数，它会保存请求结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// saga.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; take, put, call &#125; <span class="keyword">from</span> <span class="string">'redux-saga/effects'</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span>* <span class="title">mySaga</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">yield</span> take(USER_INTERACTED_WITH_UI_ACTION);</span><br><span class="line">    <span class="keyword">yield</span> put(SHOW_LOADING_ACTION, &#123;<span class="attr">isLoading</span>: <span class="literal">true</span>&#125;);</span><br><span class="line">    <span class="keyword">const</span> data = <span class="keyword">yield</span> call(GET, <span class="string">'https://my.server.com/getdata'</span>);</span><br><span class="line">    <span class="keyword">yield</span> put(SHOW_DATA_ACTION, &#123;<span class="attr">data</span>: data&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// reducer.js</span></span><br><span class="line">...</span><br><span class="line"><span class="keyword">case</span> SHOW_DATA_ACTION: <span class="function">(<span class="params">state, data</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Object</span>.assign(&#123;&#125;, state, &#123;<span class="attr">data</span>: data, <span class="attr">showLoading</span>: <span class="literal">false</span>&#125;;</span><br><span class="line">&#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>通过调用 <code>SHOW_DATA_ACTION</code> 来用接收的数据更新页面。</p>
<h2 id="刚刚发生了什么？"><a href="#刚刚发生了什么？" class="headerlink" title="刚刚发生了什么？"></a>刚刚发生了什么？</h2><p>应用启动后，所有的 Sagas 都会被执行，你可以认为一直在调用 next 方法直到结束。take 方法类似于线程挂起的作用，一旦调用了<code>USER_INTERACTED_WITH_UI_ACTION</code>，线程就会恢复执行。</p>
<p>然后，我们继续调用 <code>SHOW_LOADING_ACTION</code>，reducer 会处理这个 action。由于 Saga 还在继续运行，call 方法会发起对服务端的请求，Saga 会在再次挂起，直到请求结束。</p>
<h2 id="每次都使用"><a href="#每次都使用" class="headerlink" title="每次都使用"></a>每次都使用</h2><p>在上面的例子中，Saga 只处理了一个用户交互的 action，因为我们用 put 方法执行了 <code>SHOW_DATA_ACTION</code> 这个 action，然后后面就没有 yield 了（done 就是 true 了对吧？）。<br>如果我们希望在每次调用 <code>USER_INTERACTED_WITH_UI_ACTION</code> 这个 action 的时候，都会执行这一系列的 actions，我们可以用 while(true) 语句来包裹 Saga 内部的逻辑代码。完整代码如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// saga.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; take, put, call &#125; <span class="keyword">from</span> <span class="string">'redux-saga/effects'</span></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span> <span class="function"><span class="keyword">function</span>* <span class="title">mySaga</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"><span class="number">2.</span>   <span class="keyword">while</span> (<span class="literal">true</span>)&#123;</span><br><span class="line"><span class="number">3.</span>    <span class="keyword">yield</span> take(USER_INTERACTED_WITH_UI_ACTION);</span><br><span class="line"><span class="number">4.</span>    <span class="keyword">yield</span> put(SHOW_LOADING_ACTION, &#123;<span class="attr">isLoading</span>: <span class="literal">true</span>&#125;);</span><br><span class="line"><span class="number">5.</span>    <span class="keyword">const</span> data = <span class="keyword">yield</span> call(GET, <span class="string">'https://my.server.com/getdata'</span>);</span><br><span class="line"><span class="number">6.</span>    <span class="keyword">yield</span> put(SHOW_DATA_ACTION, &#123;<span class="attr">data</span>: data&#125;);</span><br><span class="line"><span class="number">7.</span>  &#125;</span><br><span class="line"><span class="number">8.</span> &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// reducer.js</span></span><br><span class="line">...</span><br><span class="line"><span class="keyword">case</span> SHOW_LOADING_ACTION: <span class="function">(<span class="params">state, isLoading</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Object</span>.assign(&#123;&#125;, state, &#123;<span class="attr">showLoading</span>: isLoading&#125;);</span><br><span class="line">&#125;,</span><br><span class="line"><span class="keyword">case</span> SHOW_DATA_ACTION: <span class="function">(<span class="params">state, data</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Object</span>.assign(&#123;&#125;, state, &#123;<span class="attr">data</span>: data, <span class="attr">showLoading</span>: <span class="literal">false</span>&#125;;</span><br><span class="line">&#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>这个无限循环不会造成堆栈溢出，也不会使你的应用崩溃！因为 <code>take</code> 方法就像线程挂起一样，<code>mySaga</code> 执行后会一直保持 <code>pending</code> 状态，直到那个 <code>action</code> 被触发。下次重新进入循环后，也会重复上述过程。</p>
<ul>
<li>让我们一步步地看一下上面的过程：</li>
</ul>
<p>1.应用启动，执行所有 Sagas。<br>2.mySaga 运行，进入 while(true) 循环，在第 3 行挂起。<br>3.USER_INTERACTED_WITH_UI_ACTION 这个 action 被触发。<br>4.Saga 的线程激活，执行第 4 行，触发 SHOW_LOADING_ACTION 这个 action，然后分配的 reducer 进行处理（reducer 处理后，页面就会显示 loading 提示）。<br>5.发送一个请求到服务端（第 5 行），然后会再次挂起，直到请求的 Promise 变为 resolved，请求结果的数据会赋值给 data 变量。<br>6.SHOW_DATA_ACTION 接收 data 作为参数被触发，然后 reducer 就可以使用这些数据来更新页面。<br>再次进入循环，回到第 2 步。</p>
<h2 id="接下来"><a href="#接下来" class="headerlink" title="接下来"></a>接下来</h2><p>在这篇文章中，我们介绍了 Redux-Saga 相关的基本概念，展示了如何在 React 应用中使用它。下篇文章中，我会展示在实际应用中使用它获得的价值。</p>
<p><a href="https://juejin.im/post/5ac1cb9d6fb9a028cf32a046" target="_blank" rel="noopener">原文地址</a></p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Generators-先行！"><span class="toc-number">1.1.</span> <span class="toc-text">Generators 先行！</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#什么是-Sagas？"><span class="toc-number">1.2.</span> <span class="toc-text">什么是 Sagas？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#常见用例"><span class="toc-number">1.3.</span> <span class="toc-text">常见用例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#刚刚发生了什么？"><span class="toc-number">1.4.</span> <span class="toc-text">刚刚发生了什么？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#每次都使用"><span class="toc-number">1.5.</span> <span class="toc-text">每次都使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#接下来"><span class="toc-number">1.6.</span> <span class="toc-text">接下来</span></a></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
